<?php
/**
 * Created by PhpStorm.
 * User: superxing-cto
 * Date: 17/2/10
 * Time: 15:06
 */

namespace App\Models;

use App\Http\Helper\AliyunPush;

/**
 * Class Message
 * @property integer user_id 用户ID
 * @property string content 消息内容
 * @property string icon 图标链接
 * @property boolean unread 是否已读
 * @property string url_schema 消息跳转URL Schema
 * @property string type 消息类型 system、purchase
 * @property string push_id 阿里云推送后返回的消息ID
 * @property string device_id 推送消息的设备ID
 * @property integer type_id 类型ID 如：订单ID
 */
class Message extends ApplicationModel
{
    protected $fillable = [
        'user_id',
        'content',
        'icon',
        'unread',
        'url_schema',
        'type',
        'type_id',
        'push_id',
        'device_id',
    ];
    const PRODUCT_REJECT = "您申请的“%s”服务未通过审核，原因可能涉及以下几点：\n1、未实名认证，提交的头像非本人真实头像；\n2、提交的服务照片、语音与服务描述、服务名称不相符或图片模糊、图片含有广告、LOGO水印及联系方式；\n3、提交的服务语音、照片或说明涉嫌恶搞、色情、暴力或血腥； \n4、未上传游戏段位图或段位图不清晰。\n请点击修改、重新申请。";//商品审核失败推送信息
    const PRODUCT_PUSH_REJECT = "您申请的“%s”服务未通过审核，请前往CC星球APP服务管理中申请修改";//商品审核失败推送信息
    const PRODUCT_SUCCESS = "恭喜您！您申请%s服务服务已通过审核！";//商品审核通过推送信息
    const AUDIT_IDENTITY_REJECT = "";//实名认证失败推送信息
    const AUDIT_IDENTITY_SUCCESS = "";//实名认证成功推送信息
    const PURCHASE_ACCEPT = "您已成功约到%s，服务时间%s，卖家会尽快与你取得联系，祝你们有一段美好的体验！。";//接受订单推送消息
    const PURCHASE_REJECT = "抱歉哦，你预约的%s服务，由于卖家现在不方便接单，您可以重新预约其他卖家来体验服务。";//拒绝订单推送消息
    const PURCHASE_PAYED = "您有一个新订单，请尽快处理。";//付款成功推送消息
    const PURCHASE_FINISH = "买家已经确认完成服务，这是一段美丽的经历。";//订单完成推送消息
    const WITHDRAW_AUDIT = "您申请的提现已经进入审核状态，请您耐心等待！";//申请提现提交推送消息
    const WITHDRAW_SUCCESS = "您于%s月%s日%s时%s分申请的提现已通过，请注意您的账户余额变动。";//提现审核成功推送消息
    const WITHDRAW_FAIL = "抱歉，您的提现申请失败，请重新提现或联系客服。";//提现审核失败推送消息
    const ADMIN_CANCEL_BUYER = "小c提醒，您好，您已成功取消%s的服务订单，订单号%s，我们将1个工作日内原路退款，如有问题请联系客服（微信：ccxingqiu95）感谢您的支持！";
    const ADMIN_CANCEL_SELLER = "小c提醒，您好，您已成功取消%s的服务订单，订单号%s，如有问题请联系客服（微信：ccxingqiu95）感谢您的支持！";
    const STAR_LEVEL = "您获得了新的称号“%s”，快去看看！”";

    const WORK_FOR_IDOL = "不辞辛劳给idol%s%s，获得%s钻石。";
    const IDOL_QUIT = "您的idol	%s已退出演艺圈，归隐山林。作为补偿赠送给你%s个胖C。";

    const SOCIA_STATUS_NEW_FANS = "恭喜，%s成为了您的粉丝，您的身价提升了10！";
    const SOCIA_STATUS_DO_WORKS = "繁忙的一天终于结束啦！知道吗？在粉丝疯狂打call后，您的身价又提升了%s！";

    const SOCIA_STATUS_UP_SERVICE = "又有新的服务上架啦～身价增加了10！";
    const SOCIA_STATUS_DOWN_SERVICE = "抱歉～您的服务%s下架了，身价降低了10。";
    const SOCIA_STATUS_GET_C = "粉丝%s为您赠送了%s个胖C，身价增加了%s！";
    const SOCIA_STATUS_PURCHASE = "通过您的努力，又成交了一单服务～身价提升了%s！";
    const SOCIA_STATUS_FANS_CANCEL = "抱歉，粉丝%s对您粉转路了，您的身价降低了10。";

    const QUESTION_PAYED = "%s用红包砸了你一下，快来收红包～点击查看";
    const DELETE_VIDEO = "由于您发布的视频涉嫌违规，已被删除";
    const DELETE_ANSWER_VIDEO = "由于%s发布的视频涉嫌违规，已被删除";

    const ANSWER = "%s回答了你的视频提问，快来看看吧";

    const ICON = "https://img.superxing.cc/api/img/new_icon_push.png";
    const IDOL_QUIT_ICON = "http://xc-website.oss-cn-shanghai.aliyuncs.com/api/img/idol_message_quit.png";
    const IDOL_WORK_ICON = "http://xc-website.oss-cn-shanghai.aliyuncs.com/api/img/idol_message_work.png";

    const SOCIA_STATUS_UP_ICON = "http://xc-website.oss-cn-shanghai.aliyuncs.com/api/img/rank_up.png";
    const SOCIA_STATUS_DOWN_ICON = "http://xc-website.oss-cn-shanghai.aliyuncs.com/api/img/rank_down.png";

    public function purchase()
    {
        return $this->belongsTo(Purchase::class, 'type_id')->withTrashed();
    }

    public function pushMessageType($user_id, $body, $params = "{}")
    {
        $push = new AliyunPush();
        $user_device = UserDevice::where('user_id', $user_id)->orderBy('updated_at', 'desc')->first();

        if (!$user_device) {
            return false;
        }
        if ($user_device->device_type == 'ios') {
            // \Log::info($user_device, ['Message Model pushMsg user_device']);
            $count = Message::where('user_id',$user_id)->where('unread',false)->count();
            if($count == 0){
                $count = 1;
            }
            $res = $push->pushIOSMsg('CC星超', $body, $user_device->device_id, $count, $params);
            if ($res->MessageId && !empty($this->id)) {
                date_default_timezone_set('Asia/shanghai');
                $this->push_id = $res->MessageId;
                $this->device_id = $user_device->device_id;
                $this->save();
            }
            //$res = $push->pushIOS('CC星超', $body, $user_device->user_id, $count, $params);
        }

        return true;


    }
    public function  pushMsg($user_id, $body, $params = "{}")
    {
        $push = new AliyunPush();
        $user_device = UserDevice::where('user_id', $user_id)->orderBy('updated_at', 'desc')->first();

        if (!$user_device) {
            return false;
        }
        if ($user_device->device_type == 'ios') {
           // \Log::info($user_device, ['Message Model pushMsg user_device']);
            $count = Message::where('user_id',$user_id)->where('unread',false)->count();
            if($count == 0){
                $count = 1;
            }
           $res = $push->pushIOS('CC星超', $body, $user_device->device_id, $count, $params);
          //$res = $push->pushIOS('CC星超', $body, $user_device->user_id, $count, $params);
        }
        if ($user_device->device_type == 'android') {

           //$res = $push->pushAndroid('CC星超', $body, $user_device->device_id, $params);
            $res = $push->pushAndroid('CC星超', $body, $user_device->user_id, $params);
        }
//        if ($res->ResponseId && !empty($this->id)) {
//            date_default_timezone_set('Asia/shanghai');
//            $this->push_id = $res->ResponseId;
//            $this->device_id = $user_device->device_id;
//            $this->save();
//        }
        if ($res->MessageId && !empty($this->id)) {
            date_default_timezone_set('Asia/shanghai');
            $this->push_id = $res->MessageId;
            $this->device_id = $user_device->device_id;
            $this->save();
        }
        return true;
    }

    public function pushMoreMsg($device_ids, $type, $body, $params = "{}")
    {
        $push = new AliyunPush();
        if ($type == 'ios') {
            foreach ($device_ids as $device_id) {
                $ids = implode(',', $device_id);
                $push->pushIOS('CC星超', $body, $ids, 1, $params);
            }
        }
        if ($type == 'android') {
            foreach ($device_ids as $device_id) {
                $ids = implode(',', $device_id);
                $push->pushAndroid('CC星超', $body, $ids, $params);
            }
        }
        return true;
    }

    public function pushMessageTypeMoreMsg($device_ids, $type, $body, $params = "{}")
    {
        $push = new AliyunPush();
        if ($type == 'ios') {
            foreach ($device_ids as $device_id) {
                $ids = implode(',', $device_id);
                $push->pushIOSMsg('CC星超', $body, $ids, 1, $params);
            }
        }

        return true;
    }
}